﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDX.ChangeTracking
{
    public class ChangeRecorderLocator
    {
        public IChangeRecorder Recorder { get; private set; }
        public Editor Target { get; private set; }

        public ChangeRecorderLocator(Editor editor)
        {
            this.Target = editor;

            editor.DependencyPropertyChanged += new System.Windows.DependencyPropertyChangedEventHandler(editor_DependencyPropertyChanged);
            Relocate();
        }

        void editor_DependencyPropertyChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == Scope.ChangeTrackingScopeProperty)
            {
                Relocate();
            }
        }

        void Relocate()
        {
            var scope = Scope.GetChangeTrackingScope(Target);
            var recorder = scope as IChangeRecorder;
            if (recorder != null)
            {
                Recorder = recorder;
            }
        }
    }
}
